package com.examplemartin.jpademo.controller.cust;


import com.examplemartin.jpademo.bean.AjaxResult;
import com.examplemartin.jpademo.pojo.po.cust.CustPO;
import com.examplemartin.jpademo.service.CustService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/cust")
public class CustomerConntroller {


    @Resource
    private CustService custService;

    /**
     * 跳转到顾客列表
     *
     * @param model
     *
     * @return
     */
    @GetMapping("/custlist.do")
    public String custlistDo(Model model) {

        model.addAttribute("title", "顾客列表表");

        List<CustPO> all = custService.findAll();
        model.addAttribute("custlist", all);
        return "customer/custlist";
    }

    /**
     * 跳转到新增顾客界面
     * /cust/addcust.do
     *
     * @param model
     *
     * @return
     */
    @GetMapping("/addcust.do")
    public String addcustDo(Model model) {
        model.addAttribute("title", "新增顾客");
        return "customer/addcustomer";
    }


    /**
     * 新增顾客 ajax
     * /addcust.ajax
     *
     * @param custPO
     *
     * @return
     */
    @PostMapping("/addcust.ajax")
    @ResponseBody
    public AjaxResult addcustAjax(@RequestBody CustPO custPO) {

        AjaxResult ajaxResult = custService.addcustAjax(custPO);

        return ajaxResult;
    }

    /**
     * 禁用顾客
     * @param custPO
     * @return
     */
    @PostMapping("/forbidden.ajax")
    @ResponseBody
    public AjaxResult forbiddenAjax(@RequestBody CustPO custPO){

        AjaxResult ajaxResult = custService.forbiddenAjax(custPO);
        return ajaxResult;
    }

    /**
     * 跳转到顾客编辑页面
     * /cust/editcust.do
     * @param id
     * @return
     */
    @GetMapping("/editcust.do")
    public String editcustDo(Long id,Model model){
        model.addAttribute("title","顾客信息编辑");

        CustPO byId = custService.findById(id);

        model.addAttribute("custPO",byId);
        return "customer/editcustomer";
    }

    /**
     * 修改 顾客信息
     * /editcust.ajax
     * @param custPO
     * @return
     */
    @PostMapping("/editcust.ajax")
    @ResponseBody
    public AjaxResult editcustAjax(@RequestBody CustPO custPO){

        AjaxResult ajaxResult = custService.editcustAjax(custPO);


        return ajaxResult;
    }
}



